/*
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//      Copyright (c) 2006-2007 Intel Corporation. All Rights Reserved.
//
//
*/
#include "umc_defs.h"
#if defined (UMC_ENABLE_DVHD_VIDEO_DECODER)

#include "umc_dv100_decoder.h"
#include <ippvc.h>

namespace UMC
{

static Ipp32s MacroBlockWidth = 16;
static Ipp32s MacroBlockHeight = 16;
static Ipp32s MacroBlockHalfHeight = 8;
static Ipp32s SuperBlockWidth = 9 * MacroBlockWidth;

static
void InitializeOffsets(Ipp8u **pDestinations,
                       Ipp8u *pDestImage,
                       size_t nDestPitch,
                       Ipp32s nChannelNum,
                       Ipp32s nDIFSeqNum,
                       Ipp32s nVideoSegmentNum,
                       Ipp32s nBottomMBWidth = 32)
{
    Ipp32s BytesPerPixel = 2;
    Ipp32s MBNum;
    if(nDIFSeqNum < 11)
    {
        //Main Unit
        Ipp32s i = (nVideoSegmentNum + 27*nDIFSeqNum) % 11;
        Ipp32s k = (nVideoSegmentNum + 27*nDIFSeqNum) / 11;
        Ipp32s h = nChannelNum;
        Ipp32s SuperBlockVertOrder[]=
        {
            (4*h + i + 2) % 11,
            (4*h + i + 6) % 11,
            (4*h + i + 8) % 11,
            (4*h + i + 0) % 11,
            (4*h + i + 4) % 11
        };
        Ipp32s SuperBlockHorzOrder[] = {2, 1, 3, 0, 4};
        Ipp32s Row = k/9;
        Ipp32s Col = k%9;

        for(MBNum=0; MBNum<5; MBNum++)
        {
            pDestinations[2*MBNum] = pDestImage +
                            MacroBlockHeight * nDestPitch + //Skip A0
                            (h/2 + SuperBlockVertOrder[MBNum]*6 + Row*2) * MacroBlockHeight * nDestPitch +
                            ((h%2 + SuperBlockHorzOrder[MBNum]*2 ) * SuperBlockWidth + Col * MacroBlockWidth )* BytesPerPixel;
            pDestinations[2*MBNum+1] = pDestinations[2*MBNum] + MacroBlockHalfHeight * nDestPitch;
        }
    }
    else
    {
        //Edge unit
        for(MBNum=0; MBNum<5; MBNum++)
        {
            if( (nVideoSegmentNum + MBNum*27) < 90)
            {
                //Macro block is from A0
                pDestinations[2*MBNum] = pDestImage + (nVideoSegmentNum + MBNum*27)*MacroBlockWidth*BytesPerPixel;
                pDestinations[2*MBNum+1] = pDestinations[2*MBNum] + MacroBlockHalfHeight * nDestPitch;
            }
            else
            {
                //Macro block is from A1
                pDestinations[2*MBNum] = pDestImage +
                                   67 * MacroBlockHeight * nDestPitch + //Skip A0 and main unit
                                   ((nVideoSegmentNum + MBNum*27) - 90) * nBottomMBWidth * BytesPerPixel;
                pDestinations[2*MBNum+1] = pDestinations[2*MBNum] + MacroBlockWidth * BytesPerPixel;
            }
        }
    }
}

void DV100VideoDecoder::Store_System1080_50i_Segment(Ipp16s* pDecodedVideoSegment, Ipp32s nChannelNum, Ipp32s nDIFSeqNum, Ipp32s nVideoSegmentNum)
{
    Ipp8u *pDestinations[10];
    // reset pointers
    if (0 == m_shortCutModeFlag)
    {
        InitializeOffsets(pDestinations, m_pDestination, m_nDestPitch, nChannelNum, nDIFSeqNum, nVideoSegmentNum);
        ippiYCrCb422ToYCbCr422_10HalvesMB16x8_DV100_16s8u_P3C2R(pDecodedVideoSegment, pDestinations, m_nDestPitch);
    }
    else if (1 == m_shortCutModeFlag)
    {
        MacroBlockWidth = 8;
        MacroBlockHeight = 8;
        MacroBlockHalfHeight = 4;
        SuperBlockWidth = 9 * MacroBlockWidth;
        InitializeOffsets(pDestinations, m_pDestination, m_nDestPitch, nChannelNum, nDIFSeqNum, nVideoSegmentNum, 16);
        QuarterResolutionStoreYCrCb422ToYCbCr422_DV100_16s8u_P3C2R(pDecodedVideoSegment, pDestinations, m_nDestPitch);
    }
    else if (2 == m_shortCutModeFlag)
    {
        MacroBlockWidth = 4;
        MacroBlockHeight = 4;
        MacroBlockHalfHeight = 2;
        SuperBlockWidth = 9 * MacroBlockWidth;
        InitializeOffsets(pDestinations, m_pDestination, m_nDestPitch, nChannelNum, nDIFSeqNum, nVideoSegmentNum, 8);
        Resolution8x8To4x4To2x2StoreYCrCb422ToYCbCr422_DV100_16s8u_P3C2R(pDecodedVideoSegment, pDestinations, m_nDestPitch);
    }
    else if (3 == m_shortCutModeFlag)
    {
        MacroBlockWidth = 2;
        MacroBlockHeight = 2;
        MacroBlockHalfHeight = 1;
        SuperBlockWidth = 9 * MacroBlockWidth;
        InitializeOffsets(pDestinations, m_pDestination, m_nDestPitch, nChannelNum, nDIFSeqNum, nVideoSegmentNum, 4);
        DCOnlyStoreYCrCb422ToYCbCr422_DV100_16s8u_P3C2R(pDecodedVideoSegment, pDestinations/*, m_nDestPitch*/);
    }
    else if (4 == m_shortCutModeFlag)
    {
        MacroBlockWidth = 16;
        MacroBlockHeight = 8;
        MacroBlockHalfHeight = 4;
        SuperBlockWidth = 9 * MacroBlockWidth;
        InitializeOffsets(pDestinations, m_pDestination, m_nDestPitch, nChannelNum, nDIFSeqNum, nVideoSegmentNum);
        Resolution8x8To8x4StoreYCrCb422ToYCbCr422_DV100_16s8u_P3C2R(pDecodedVideoSegment, pDestinations, m_nDestPitch);
    }
    else if (5 == m_shortCutModeFlag)
    {
        MacroBlockWidth = 16;
        MacroBlockHeight = 4;
        MacroBlockHalfHeight = 2;
        SuperBlockWidth = 9 * MacroBlockWidth;
        InitializeOffsets(pDestinations, m_pDestination, m_nDestPitch, nChannelNum, nDIFSeqNum, nVideoSegmentNum);
        Resolution8x8To8x2StoreYCrCb422ToYCbCr422_DV100_16s8u_P3C2R(pDecodedVideoSegment, pDestinations, m_nDestPitch);
    }
}


}//namespace UMC

#endif //(UMC_ENABLE_DVHD_VIDEO_DECODER)
